package zj.com.activities;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import zj.com.adapter.GsInputSelectAdapter;
import zj.com.model.User;
import zj.com.net.NetManager;
import zj.com.net.OnResponseListener;
import zj.com.util.DialogUtil;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/21.
 *
 * 输入
 */
public class GsSelectInputActivity extends  BaseActivity implements View.OnClickListener{


    public TextView title_name,right_tv;
    public ImageView left_iv;
    public ListView listView;
    public EditText search_et;
    public List<User> list;
    public GsInputSelectAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gs_select_input);
        initView();
    }

    public void initView(){
        title_name = (TextView) findViewById(R.id.title_name);
        title_name.setText("选择");
        left_iv = (ImageView) findViewById(R.id.left_iv);
        left_iv.setVisibility(View.VISIBLE);
        left_iv.setOnClickListener(this);

        right_tv = (TextView) findViewById(R.id.right_tv);
        right_tv.setText("确定");
        right_tv.setVisibility(View.VISIBLE);
        right_tv.setOnClickListener(this);

        search_et = (EditText) findViewById(R.id.search_et);

        listView = (ListView) findViewById(R.id.listView);
        list = new ArrayList<>();
        adapter = new GsInputSelectAdapter(list,this);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent();
                        intent.putExtra("user",list.get(position));
                setResult(RESULT_OK,intent);
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.left_iv:
                finish();
                break;
            case R.id.right_tv:
                search();
                break;
        }
    }

    public ProgressDialog dialog;
    public void search(){

        String keyword = search_et.getText().toString();
        if(TextUtils.isEmpty(keyword)){

            Toast.makeText(this, "关键字不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        dialog = DialogUtil.createProgressDialog(this,"提示","数据搜索中...");
        NetManager.getInstance(this).GetUserListByKeyWord(keyword,"Inspect","", new OnResponseListener<List<User>>() {
            @Override
            public void onComplete(boolean isOK, List<User> result, int errorCode, String errorMsg) {

                dialog.dismiss();
                if(isOK){

                    if(result == null || result.size() == 0){

                        Toast.makeText(GsSelectInputActivity.this, "没有你搜索的数据", Toast.LENGTH_SHORT).show();

                        return;
                    }
                    list = result;
                    adapter.setDataChange(result);
                }else{
                    Toast.makeText(GsSelectInputActivity.this, "搜索失败", Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {

                dialog.dismiss();
                Toast.makeText(GsSelectInputActivity.this, "搜索失败", Toast.LENGTH_SHORT).show();

            }
        });
    }
}
